"use strict";
/* Controllers */
app.controller("traceInfoListController", ['$scope', '$location', '$rootScope', '$routeParams', 'traceInfoListService', function ($scope, $location, $rootScope, $routeParams, traceInfoListService) {

    var vm = $scope.vm;
    vm.p = vm.result0=vm.result={};
    vm.p.infoId = $routeParams.id;

    if(vm.p.infoId==0) {
        vm.p.infoId = '47951791977764';
    }

    vm.p.startTime = hll2StdTime(getHBefore(23) + "0000");
    vm.p.endTime = hll2StdTime(getHMBefore(-1)+"00");
    vm.p.hasExp = false;

    $('.date').datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD HH:mm:00'
    });
    $('.date').on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input')
        var eName = currentInput.attr('name')
        var p = $scope.vm.p
        p[eName] = currentInput.val()
        $scope.$apply()
    })

    $("#startTime").on("dp.change", function (e) {
        $('#endTime').data("DateTimePicker").minDate(e.date);
    });
    $("#endTime").on("dp.change", function (e) {
        $('#startTime').data("DateTimePicker").maxDate(e.date);
    });

    var errorFn = function (result) {
        alert(result);
    };

    $scope.findTraceInfo = function () {
        var vm = $scope.vm;
        vm.result.ms = '...';
        var params = {
            ts1: vm.p.startTime,
            ts2: vm.p.endTime,
            infoId: vm.p.infoId,
            hasExp: vm.p.hasExp
        };
        $('#list_table').bootstrapTable('showLoading');
        $("#btn").addClass('disabled');
        var procFn = function (response) {
            vm.result =  response;
            if (vm.result.data == null) {
                alert("请求失败：" + response.message)
            }else {
                $('#list_table').bootstrapTable('load', vm.result.data);
            }
            $('#list_table').bootstrapTable('hideLoading');
            $("#btn").removeClass('disabled');
        };
        traceInfoListService.findTraceInfo(params, procFn, errorFn);
    };

    $scope.queryList = function () {
        var vm = $scope.vm;
        var d1 = new Date(vm.p.startTime);
        var d2 = new Date(vm.p.endTime);
        var diff = parseInt(d2 - d1) / 1000 / 60 / 60;
        if(diff>90) {
            alert("时间范围超过3天，请保持在3天内");
            return;
        }
        $scope.findTraceInfo();
        $scope.queryTraceInfoTags();
    };

    $scope.queryTraceInfoTags = function() {
        var vm = $scope.vm;
        var params = {
            infoId: vm.p.infoId
        };
        var procFn = function (res) {
            if (res.data == null) {
                alert("请求失败：" + res.message)
                return;
            }
            vm.p.infoTitle = res.data.infoTitle;
        };
        traceInfoListService.queryTraceInfoTags(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 100, 1000], pageSize: 30
        });

//        $resultTable.bootstrapTable('hideColumn', 'adDispType');
//        $resultTable.bootstrapTable('hideColumn', 'slot');
//        $resultTable.bootstrapTable('hideColumn', 'tjfrom');
    };
    $scope.initializeController();

}]);

/* Services */
app.service('traceInfoListService', ['ajaxService', function (ajaxService) {

    this.findTraceInfo = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/user_trace/findTraceImc.do", successFunction, errorFunction);
    };

    this.queryTraceInfoTags = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/user_trace/queryTraceInfoTags.do", successFunction, errorFunction);
    };
}]);




function userIdFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        row.userName,'(',
        value,')',
        " <a href='http://zpknowledge-graphprofilevis.58corp.com/userportrait' target='_blank'>查看画像...</a>"
    ].join('');
    return value;
}


function dispLocal1Fmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var name = '其他';
    if(row.dispLocal1Name != null) {
        name = row.dispLocal1Name;
    }
    switch(value) {
        case '1':
            name='北京'
            break;
        default:
            break;
    }
    return [
        name, '(',value,')'
    ].join('');
}

function platformFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var biz = '其他';
    if(row.biz=='ganji') {
        biz = '赶集';
    }else if(row.biz=='58'){
        biz='58';
    }
    return [
        biz, value
    ].join('');
}


function isSharedFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var isShared = value.indexOf('share');
    if(isShared==-1) {
        return '否';
    }else {
        return '是（'+value+'）'
    }
}
